package com.atguigu.srb.core.controller.api;


import com.atguigu.common.exception.BusinessAssert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.util.RegexValidateUtils;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVO;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.service.UserInfoService;
import com.atguigu.srb.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author 龙龙龙
 * @since 2022-01-21
 */
@RestController
@RequestMapping("/api/core/userInfo")
//@CrossOrigin
@Slf4j
@Api(tags = "用户操作")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("checkMobile/{mobile}")
    public boolean checkMobile(@PathVariable("mobile") String mobile){
        boolean b = userInfoService.checkMobile(mobile);
        return b;
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public R register(@RequestBody RegisterVO registerVO) {
        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        String code = registerVO.getCode();

        //MOBILE_NULL_ERROR(-202, "手机号不能为空"),
        BusinessAssert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        //MOBILE_ERROR(-203, "手机号不正确"),
        BusinessAssert.isTrue(RegexValidateUtils.checkCellphone(mobile), ResponseEnum.MOBILE_ERROR);
        //PASSWORD_NULL_ERROR(-204, "密码不能为空"),
        BusinessAssert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        //CODE_NULL_ERROR(-205, "验证码不能为空"),
        BusinessAssert.notEmpty(code, ResponseEnum.CODE_NULL_ERROR);

        userInfoService.register(registerVO);

        return R.ok();
    }


    @ApiOperation("会员登录")
    @PostMapping("/login")
    public R login(@RequestBody LoginVO loginVO, HttpServletRequest request) {
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        boolean b = RegexValidateUtils.checkCellphone(mobile);
        BusinessAssert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        BusinessAssert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        BusinessAssert.isTrue(b, ResponseEnum.MOBILE_ERROR);

        //获得ip地址
        String ip = getIpAddr(request);

        UserInfoVO userInfoVO = userInfoService.login(loginVO, ip);
        return R.ok().data("userInfo", userInfoVO);

    }

    @GetMapping("checkToken")
    public R checkToken(HttpServletRequest request){
        String token = request.getHeader("token");
        BusinessAssert.notNull(token,ResponseEnum.WEIXIN_FETCH_USERINFO_ERROR);
        boolean b = JwtUtils.checkToken(token);
        BusinessAssert.isTrue(b,ResponseEnum.WEIXIN_FETCH_USERINFO_ERROR);
        Long userId = JwtUtils.getUserId(token);
        String userName = JwtUtils.getUserName(token);

        return R.ok();
    }

    /**
     * 获取ip地址
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            ip = "127.0.0.1";
        }
        if (ip.split(",").length > 1) {
            ip = ip.split(",")[0];
        }
        return ip;
    }
}

